<script type="text/javascript">
	
	
</script>
<?php if(AuthComponent::user()){
	$userLogin = AuthComponent::user();
}else{
	$userLogin = null;
}?>
<div class="col-sm-5 col-md-4 sidebar">
				<div align="center">
					<h1>
						<span class="text-left text-custom"><?php echo $user['User']['username']?>'s Blog</span>
					</h1>
					<img
						src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTJfM-cz_DDwZpaT9SNHNSHJpMhdTbPVbM62bFPEDGREECUmcZptg"
						class="img-circle" style="max-height: 100px; max-width: 100px;">
				</div>
				<br>
				<!-- statistic -->
				<div class="col-md-offset-1">
					<p class="text-info">
						Fullname: <strong><?php echo $user['User']['fullname']?></strong>
					</p>
					<p class="text-info">
						Posted: <strong><?php echo count($created)?></strong> post(s)
					</p>
					<p class="text-info">
						Last posted: <strong>
				<?php
				if ($created) {
					$total = max ( $created );
					echo $total ['Post'] ['created'];
				} else {
					echo 'This user has no post.';
				}
				?></strong>
					</p>
					<!-- graph -->
					<div id="chart_div" class=""></div>
				</div>
</div>

<div class="col-sm-7 col-sm-offset-5 col-md-8 col-md-offset-4 main">
	<?php if($isAuthor):?>
		<h2 class="sub-header"><a href="/adminposts/add/" class="btn btn-default">Add New Post </a></h2>
	<?php endif;?>
	<div align="right">
					<ul class="pagination pagination-sm">
						<li><?php echo $this->Paginator->first('<<< First',array('class' => 'active'));?></li>
						<li><?php echo $this->Paginator->prev('<<', array('class' => 'active'), null, array('class'=>'hidden'));?></li>
						<li><?php echo $this->Paginator->numbers();?></li>
						<li><?php echo $this->Paginator->next('>>', array('class' => 'active'), null, array('class'=>'hidden'));?></li>
						<li><?php echo $this->Paginator->last('Last >>>',array('class' => 'active'));?></li>
					</ul>
	</div>
    <div class="table-responsive">
            <table class="table table-striped">
              <thead>
                <tr>
                  <th>#</th>
                  <th>Title</th> 
                  <th>Last Modified </th>                 
                  <th style="width: 150px; text-align: center;">Option</th>
                </tr>
              </thead>
             
              <tbody>
               <?php
               $output=''; $no = ($this->Paginator->current ( 'Post' ) - 1) *5;
              	foreach ($posts as $key => $value) {
					$no+=1;
					$output.='<tr>';
					$output.='<td>'.$no .'</td>';
					$output.='<td> <a href="/posts/detail/'. $value['Post']['id'] . '" >' . $value['Post']['title'].'</a> </td>';
					$output.= '<td>'. $value['Post']['modified'] . '</td>';					
					//$output.='<td>'.$value['Post']['created'].'</td>';
					//$output.='<td>'.$value['Post']['modified'].'</td>';
					if ($isAuthor || ($userLogin!=null && $userLogin['Group']['name']=='admins')){
						$output.='<td style="text-align: center;">
							<button type="button" class="btn btn-default"
								onclick="update('.$value['Post']['id'].')">
								<span class="glyphicon glyphicon-edit"></span>
							</button>

							<button type="button"
								data-id="'.$value['Post']['id'].'"
								class="btn btn-c btnDel btn-default" data-toggle="modal"
								data-target=".bs-example-modal-sm">
								<span class="glyphicon glyphicon-remove"></span>
							</button>
						</td>';
					}else{
						$output.="<td></td> <td></td>";
					}
					
					$output.='</tr>';

				}
				echo $output;
              ?>	
                
         </tbody>
     </table>
 </div>
 </div>
<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog"
	aria-labelledby="myLargeModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-sm">
		<div class="modal-content">

			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"
					aria-hidden="true">&times;</button>
				<h4 class="modal-title" id="myModalLabel">Confirm Delete</h4>
			</div>
			<div class="modal-body">
				Do you realy want to delete this record? <input type="hidden"
					id="delID" value="" />

			</div>
			<div class="modal-footer">
				<div class="btn-group btn-group-justified">
					<div class="btn-group">
						<button type="button" class="btn btn-custom"
							onclick="removePost()">Delete</button>
					</div>
					<div class="btn-group">
						<button type="button" class="btn btn-inverse" data-dismiss="modal">Close</button>
					</div>
				</div>

			</div>
		</div>
	</div>
</div>

 <!-- <script type="text/javascript" src="https://www.google.com/jsapi"></script> -->
<script type="text/javascript">
function removePost(){	
	var del_ID = document.getElementById("delID").value;	
	var f = document.createElement("form");
	f.setAttribute('method',"post");
	f.setAttribute('action',"/adminposts/delete/" + del_ID);
	f.submit();
}

function update(id){	
	document.location.href = "/adminposts/edit/" + id;
}

$(document).on("click", ".btnDel", function () {	
    var delID = $(this).data('id');
    $(".modal-body #delID").val( delID );
});

$(document).ready(function() { 
	$('table').tablesorter(); 
}); 	

google.load('visualization', '1.0', {'packages':['corechart']});

google.setOnLoadCallback(drawChart);

function drawChart() {
    var data = new google.visualization.DataTable(<?php echo $chartData;?>);
	
    var options = {
      title: 'Posts by Date',
      colors: ['#9b7188'],
      backgroundColor: '#eeeee6',
      width:350,
      height:200
    };

    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
</script>